package com.relife.core.easyui;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.relife.common.model.ClientManager;
import com.relife.common.utils.StringUtil;
import com.relife.core.entity.CodeEntity;
import org.apache.log4j.Logger;



public class CodeTag extends TagSupport {
	private static final Logger log = Logger.getLogger("CodeTag");
	private static final long serialVersionUID = 1L;
	private static final String INP_SELECT = "select";
	private static final String INP_RADIO = "radio";
	private static final String VALUE = "value";
	private static final String INP_TEXT = "text";
	private static final String INP_CHECKBOX = "checkbox";

	private String code;
	private String type;
	private String value;
	private String style = "";
	private String name;
	private String exclude;
	private String className = "";
	private String optionName = "请选择";
	private boolean showVal = false;

	@Override
	public int doStartTag() throws JspTagException {
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			sb.append(this.getHtml());
			out.print(sb);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private StringBuffer getHtml() {
		StringBuffer sb = new StringBuffer("");
		if (VALUE.equals(type)) {
			if (StringUtil.isNotEmpty(value)) {
				String[] vals = value.split(",");
				for (int i = 0; i < vals.length; i++) {
					CodeEntity c = ClientManager.getInstance().getCode(code, vals[i]);
					if (c != null) {
						sb.append((i == 0 ? "" : ",") + c.getName());
					}
				}
			}

		} else if (INP_RADIO.equals(type)) {
			List<CodeEntity> list = ClientManager.getInstance().getCodeList(code, exclude);
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getValue().equals(value)) {
						sb.append("<label><input checked=\"checked\" id=\"" + name + "\" name=\"" + name + "\" value=\"" + list.get(i).getValue() + "\"");
						sb.append("style=\"" + style + "\" class=\"" + className + "\" type=\"radio\">" + list.get(i).getName() + "</label>");
					} else {
						sb.append("<label><input id=\"" + name + "\" name=\"" + name + "\" value=\"" + list.get(i).getValue() + "\"");
						sb.append("style=\"" + style + "\" class=\"" + className + "\" type=\"radio\">" + list.get(i).getName() + "</label>");
					}
				}
			}

		} else if (INP_SELECT.equals(type)) {
			sb.append("<select id=\"" + name + "\" class=\"" + className + "\" style=\"" + style + "\" name=\"" + name + "\">");
			sb.append(" <option value=\"\">" + optionName + "</option>");
			List<CodeEntity> list = ClientManager.getInstance().getCodeList(code, exclude);
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getValue().equals(value)) {
						sb.append(" <option optname=\"" + list.get(i).getName() + "\" selected=\"selected\" value=\"" + list.get(i).getValue() + "\">" + (showVal ? list.get(i).getVal() + " " : "")
								+ list.get(i).getName() + "</option>");
					} else {
						sb.append(" <option optname=\"" + list.get(i).getName() + "\" value=\"" + list.get(i).getValue() + "\">" + (showVal ? list.get(i).getVal() + " " : "") + list.get(i).getName()
								+ "</option>");
					}
				}
			}
			sb.append("</select>");
		} else if (INP_TEXT.equals(type)) {
			CodeEntity c = ClientManager.getInstance().getCode(code, value);
			String codeName = "";
			if (c != null) {
				codeName = c.getName();
			}
			sb.append("<input id=\"" + name + "\" class=\"" + className + "\" style=\"" + style + "\" name=\"" + name + "\" value=\"" + codeName + "\">");

		} else if (INP_CHECKBOX.equals(type)) {
			List<CodeEntity> list = ClientManager.getInstance().getCodeList(code, exclude);
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					if (value.contains(list.get(i).getValue())) {
						sb.append("<label><input checked=\"checked\" id=\"" + name + "\" name=\"" + name + "\" value=\"" + list.get(i).getValue() + "\"");
						sb.append("style=\"" + style + "\" class=\"" + className + "\" type=\"checkbox\">" + list.get(i).getName() + "</label>");
					} else {
						sb.append("<label><input id=\"" + name + "\" name=\"" + name + "\" value=\"" + list.get(i).getValue() + "\"");
						sb.append("style=\"" + style + "\" class=\"" + className + "\" type=\"checkbox\">" + list.get(i).getName() + "</label>");
					}
				}
			}
		} else {
			log.error("type的参数只能能为【select,radio,checkbox,text,value】");
		}
		return sb;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOptionName() {
		return optionName;
	}

	public void setOptionName(String optionName) {
		this.optionName = optionName;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public boolean isShowVal() {
		return showVal;
	}

	public void setShowVal(boolean showVal) {
		this.showVal = showVal;
	}

	public String getExclude() {
		return exclude;
	}

	public void setExclude(String exclude) {
		this.exclude = exclude;
	}

}
